<%@include file="template/configuration.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<% pageContext.setAttribute("newLineChar", "\n"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>${website} || ${str_action}</title>
        ${stylesheet}
        ${jquery}
    </head>
    <body>
        <div class="container">
            <%@include file="template/header.jsp" %>
            <div class="content">
                <h1>${str_action} <c:if test="${action == 'view'}">email</c:if></h1>
                <c:choose>
                    <c:when test="${fn:length(private_message)>0}">
                        <c:forEach items="${private_message}" var="pm">
                            <div class="mail-block">
                                <div class="mail-top">
                                    Back to 
                                    <c:choose>
                                        <c:when test="${pm.folder == 2}">
                                            <a href="${url}PrivateMessage?folder=outbox">Outbox</a>
                                        </c:when>
                                        <c:when test="${pm.folder == 3}">
                                            <a href="${url}PrivateMessage?folder=trash">Trash</a>
                                        </c:when>
                                        <c:otherwise>
                                            <a href="${url}PrivateMessage?folder=inbox">Inbox</a>
                                        </c:otherwise>
                                    </c:choose>
                                    <c:if test="${action != 'reply'}">
                                        <div class="float-right">
                                            <c:choose>
                                                <c:when test="${pm.folder == 1}">
                                                    <a href="${url}ComposePrivateMessage?message=${pm.id}&action=reply" title="Reply"><img src="${url}images/mail-reply.png" /></a>
                                                </c:when>
                                                <c:when test="${pm.folder == 3}">
                                                    <a href="${url}ComposePrivateMessage?message=${pm.id}&action=revert" title="Revert"><img src="${url}images/mail-revert.png" /></a>
                                                </c:when>
                                            </c:choose>
                                            <a href="${url}ComposePrivateMessage?message=${pm.id}&action=delete" title="Remove" class="remove"><img src="${url}images/remove.png" /></a>
                                            <c:choose>
                                                <c:when test="${pm.folder == 3}">
                                                    <span class="info">this message definitely from <strong>${pm.sender.firstname} ${pm.sender.insertion} ${pm.sender.lastname}</strong><br />(This action cannot not be undone)</span>
                                                </c:when>
                                                <c:otherwise>
                                                    <span class="info">this message from <strong>${pm.sender.firstname} ${pm.sender.insertion} ${pm.sender.lastname}</strong></span>
                                                </c:otherwise>
                                            </c:choose>
                                        </div>
                                    </c:if>
                                </div>
                                <form method="POST" action="${url}ComposePrivateMessage">
                                    <c:choose>
                                        <c:when test="${action == 'view'}">
                                            <c:choose>
                                                <c:when test="${pm.status == 3}"> <!-- outbox //-->
                                                    <label for="receiver">To</label> <div class="information-text">${pm.receiver.firstname} ${pm.receiver.insertion} ${pm.receiver.lastname}</div>
                                                    <br />
                                                </c:when>
                                                <c:when test="${pm.status == 4}"> <!-- trash //-->
                                                    <c:choose>
                                                        <c:when test="${pm.sender == sessionScope.userdetails.id}">
                                                            <label for="receiver">To</label> <div class="information-text">${pm.receiver.firstname} ${pm.receiver.insertion} ${pm.receiver.lastname}</div>
                                                            <br />
                                                        </c:when>
                                                        <c:otherwise>
                                                            <label for="sender">From</label> <div class="information-text">${pm.sender.firstname} ${pm.sender.insertion} ${pm.sender.lastname}</div>
                                                            <br />
                                                        </c:otherwise>
                                                    </c:choose>
                                                </c:when>
                                                <c:otherwise> <!-- inbox //-->
                                                    <label for="sender">From</label> <div class="information-text">${pm.sender.firstname} ${pm.sender.insertion} ${pm.sender.lastname}</div>
                                                    <br />
                                                </c:otherwise>
                                            </c:choose>
                                            <label for="subject">Subject</label> <div class="information-text">${pm.subject}</div>
                                            <br />
                                            <label for="message">Message</label> <div class="information-text">${fn:replace(pm.message, newLineChar, "<br />")}</div>
                                        </c:when>
                                        <c:when test="${action == 'reply'}">
                                            <input type="hidden" name="reply" value="${pm.id}" />
                                            <input type="hidden" name="to" value="${pm.sender.member_id}" />
                                            <label>To</label> <div class="information-text">${pm.sender.firstname} ${pm.sender.insertion} ${pm.sender.lastname}</div>
                                            <br />
                                            <label for="subject">Subject</label> <input type="text" name="subject" id="subject" value="RE: ${pm.subject}" required>
                                            <br />
                                            <label for="message">Message</label> <textarea name="message" id="message" required>${pm.message}</textarea>
                                            <label>&nbsp;</label> <input type="submit" value="Send message">
                                        </c:when>
                                    </c:choose>
                                </form>
                            </div>
                            <div class="navigation-mail-block">
                                <div class="folder">
                                    <a href="${url}PrivateMessage?folder=inbox" <c:if test="${pm.folder == 1}">class="bold"</c:if>>Inbox</a>
                                </div>
                                <div class="folder">
                                    <a href="${url}PrivateMessage?folder=outbox" <c:if test="${pm.folder == 2}">class="bold"</c:if>>Outbox</a>
                                </div>
                                <div class="folder">
                                    <a href="${url}PrivateMessage?folder=trash" <c:if test="${pm.folder == 3}">class="bold"</c:if>>Trash</a>
                                </div>
                            </div>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <div class="mail-block">
                            <form method="POST" action="${url}ComposePrivateMessage">
                                <input type="hidden" name="to" value="${receiver_id}" />
                                <label>To</label> <div class="information-text">${receiver_fullname}</div>
                                <br />
                                <label for="subject">Subject</label> <input type="text" name="subject" id="subject" required>
                                <br />
                                <label for="message">Message</label> <textarea name="message" id="message" required></textarea>
                                <label>&nbsp;</label> <input type="submit" value="Send message">
                            </form>
                        </div>
                        <div class="navigation-mail-block">
                            <div class="folder">
                                <a href="${url}PrivateMessage?folder=inbox">Inbox</a>
                            </div>
                            <div class="folder">
                                <a href="${url}PrivateMessage?folder=outbox">Outbox</a>
                            </div>
                            <div class="folder">
                                <a href="${url}PrivateMessage?folder=trash">Trash</a>
                            </div>
                        </div>
                    </c:otherwise>
                </c:choose>
            </div>
            <%@include file="template/footer.jsp" %>
        </div>
    </body>
</html>
